package top.yokey.shopnc.activity.base;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import top.yokey.base.base.MemberHttpClient;
import top.yokey.shopnc.R;
import top.yokey.shopnc.activity.main.MainActivity;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.shopnc.base.BaseCountTime;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.shopnc.base.BaseShared;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.base.model.ConnectModel;
import top.yokey.base.model.LoginModel;
import top.yokey.base.util.JsonUtil;
import top.yokey.base.util.TextUtil;
import top.yokey.shopnc.view.CenterTextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.activity_base_register)
public class RegisterActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;

    @ViewInject(R.id.typeLinearLayout)
    private LinearLayoutCompat typeLinearLayout;
    @ViewInject(R.id.normalTextView)
    private CenterTextView normalTextView;
    @ViewInject(R.id.mobileTextView)
    private CenterTextView mobileTextView;

    @ViewInject(R.id.normalLinearLayout)
    private LinearLayoutCompat normalLinearLayout;
    @ViewInject(R.id.usernameEditText)
    private AppCompatEditText usernameEditText;
    @ViewInject(R.id.passwordEditText)
    private AppCompatEditText passwordEditText;
    @ViewInject(R.id.confirmEditText)
    private AppCompatEditText confirmEditText;
    @ViewInject(R.id.emailEditText)
    private AppCompatEditText emailEditText;
    @ViewInject(R.id.registerTextView)
    private AppCompatTextView registerTextView;

    @ViewInject(R.id.mobileLinearLayout)
    private LinearLayoutCompat mobileLinearLayout;
    @ViewInject(R.id.mobileEditText)
    private AppCompatEditText mobileEditText;
    @ViewInject(R.id.getTextView)
    private AppCompatTextView getTextView;
    @ViewInject(R.id.codeEditText)
    private AppCompatEditText codeEditText;
    @ViewInject(R.id.passwordSmsEditText)
    private AppCompatEditText passwordSmsEditText;
    @ViewInject(R.id.completeTextView)
    private AppCompatTextView completeTextView;

    private long exitTimeLong;
    private Drawable normalDrawable, normalPressDrawable;
    private Drawable mobileDrawable, mobilePressDrawable;

    @Override
    public void initData() {

        exitTimeLong = 0L;
        normalDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_normal, R.color.greyAdd);
        mobileDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_mobile, R.color.greyAdd);
        normalPressDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_normal_press);
        mobilePressDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_mobile_press);

        setToolbar(mainToolbar, "会员注册");

        getState();

    }

    @Override
    public void initEven() {

        normalTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                normalTextView.setTextColor(BaseApplication.get().getColors(R.color.primary));
                normalTextView.setCompoundDrawablesWithIntrinsicBounds(normalPressDrawable, null, null, null);
                mobileTextView.setTextColor(BaseApplication.get().getColors(R.color.greyAdd));
                mobileTextView.setCompoundDrawablesWithIntrinsicBounds(mobileDrawable, null, null, null);
                normalLinearLayout.setVisibility(View.VISIBLE);
                mobileLinearLayout.setVisibility(View.GONE);
            }
        });

        mobileTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                normalTextView.setTextColor(BaseApplication.get().getColors(R.color.greyAdd));
                normalTextView.setCompoundDrawablesWithIntrinsicBounds(normalDrawable, null, null, null);
                mobileTextView.setTextColor(BaseApplication.get().getColors(R.color.primary));
                mobileTextView.setCompoundDrawablesWithIntrinsicBounds(mobilePressDrawable, null, null, null);
                normalLinearLayout.setVisibility(View.GONE);
                mobileLinearLayout.setVisibility(View.VISIBLE);
            }
        });

        registerTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });

        getTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCode();
            }
        });

        completeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkCode();
            }
        });

    }

    @Override
    public void onReturn() {

        if (System.currentTimeMillis() - exitTimeLong > BaseConstant.TIME_EXIT) {
            BaseSnackBar.get().showClickReturn(mainLinearLayout);
            exitTimeLong = System.currentTimeMillis();
        } else {
            super.onReturn();
        }

    }

    //自定义方法

    private void getState() {

        BaseSnackBar.get().showHandler(mainLinearLayout);

        ConnectModel.get().getState("connect_sms_reg", new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                if (JsonUtil.isSuccess(baseBean.getDatas())) {
                    normalTextView.setTextColor(BaseApplication.get().getColors(R.color.primary));
                    normalTextView.setCompoundDrawablesWithIntrinsicBounds(normalPressDrawable, null, null, null);
                    mobileTextView.setTextColor(BaseApplication.get().getColors(R.color.greyAdd));
                    mobileTextView.setCompoundDrawablesWithIntrinsicBounds(mobileDrawable, null, null, null);
                    typeLinearLayout.setVisibility(View.VISIBLE);
                    normalLinearLayout.setVisibility(View.VISIBLE);
                    mobileLinearLayout.setVisibility(View.GONE);
                } else {
                    typeLinearLayout.setVisibility(View.GONE);
                    normalLinearLayout.setVisibility(View.VISIBLE);
                    mobileLinearLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

    private void register() {

        BaseApplication.get().hideKeyboard(getActivity());

        String username = usernameEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        String confirm = confirmEditText.getText().toString();
        String email = emailEditText.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirm) || TextUtils.isEmpty(email)) {
            BaseSnackBar.get().show(mainLinearLayout, "请填写完所有的信息...");
            return;
        }

        if (!password.equals(confirm)) {
            BaseSnackBar.get().show(mainLinearLayout, "两次输入的密码不一致...");
            return;
        }

        if (!TextUtil.isEmail(email)) {
            BaseSnackBar.get().show(mainLinearLayout, "邮箱地址格式不正确...");
            return;
        }

        registerTextView.setEnabled(false);
        registerTextView.setText("注册中...");

        LoginModel.get().register(username, password, email, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                registerTextView.setEnabled(true);
                registerTextView.setText("注册账号");
                BaseToast.get().show("注册成功！");
                MemberHttpClient.get().updateKey(JsonUtil.getDatasString(baseBean.getDatas(), "key"));
                BaseShared.get().putString(BaseConstant.SHARED_KEY, JsonUtil.getDatasString(baseBean.getDatas(), "key"));
                BaseApplication.get().start(getActivity(), MainActivity.class);
                BaseApplication.get().finish(getActivity());
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
                registerTextView.setText("注册账号");
                registerTextView.setEnabled(true);
            }
        });

    }

    private void getCode() {

        BaseApplication.get().hideKeyboard(getActivity());

        String mobile = mobileEditText.getText().toString();

        if (!TextUtil.isMobile(mobile)) {
            BaseSnackBar.get().show(mainLinearLayout, "手机号码格式不正确！");
            return;
        }

        getTextView.setEnabled(false);
        getTextView.setText("获取中...");

        ConnectModel.get().getSmsCaptcha("1", mobile, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                final String smsTime = JsonUtil.getDatasString(baseBean.getDatas(), "sms_time");
                final int time = Integer.parseInt(smsTime);
                //倒计时
                new BaseCountTime(time * 1000, BaseConstant.TIME_TICK) {

                    int totalTime = time;

                    @Override
                    public void onTick(long millis) {
                        super.onTick(millis);
                        String temp = "再次获取（" + totalTime-- + " S ）";
                        getTextView.setText(temp);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getTextView.setEnabled(true);
                        getTextView.setText("获取验证码");
                    }

                }.start();

            }

            @Override
            public void onFailure(String reason) {
                getTextView.setEnabled(true);
                getTextView.setText("获取验证码");
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

    private void checkCode() {

        BaseApplication.get().hideKeyboard(getActivity());

        String captcha = codeEditText.getText().toString();
        String mobile = mobileEditText.getText().toString();

        if (TextUtils.isEmpty(captcha)) {
            BaseSnackBar.get().show(mainLinearLayout, "请输入验证码！");
            return;
        }

        completeTextView.setEnabled(false);
        completeTextView.setText("处理中...");

        ConnectModel.get().checkSmsCaptcha("1", mobile, captcha, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                if (JsonUtil.isSuccess(baseBean.getDatas())) {
                    smsRegister();
                } else {
                    completeTextView.setEnabled(true);
                    completeTextView.setText("完成注册");
                    BaseSnackBar.get().showFailure(mainLinearLayout);
                }
            }

            @Override
            public void onFailure(String reason) {
                completeTextView.setEnabled(true);
                completeTextView.setText("完成注册");
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

    private void smsRegister() {

        String captcha = codeEditText.getText().toString();
        String mobile = mobileEditText.getText().toString();
        String password = passwordSmsEditText.getText().toString();

        if (TextUtils.isEmpty(password)) {
            BaseSnackBar.get().show(mainLinearLayout, "请输入账户密码！");
            return;
        }

        ConnectModel.get().smsRegister(mobile, captcha, password, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                completeTextView.setEnabled(true);
                completeTextView.setText("完成注册");
                BaseToast.get().show("注册成功！");
                MemberHttpClient.get().updateKey(JsonUtil.getDatasString(baseBean.getDatas(), "key"));
                BaseShared.get().putString(BaseConstant.SHARED_KEY, JsonUtil.getDatasString(baseBean.getDatas(), "key"));
                BaseApplication.get().start(getActivity(), MainActivity.class);
                BaseApplication.get().finish(getActivity());
            }

            @Override
            public void onFailure(String reason) {
                completeTextView.setEnabled(true);
                completeTextView.setText("完成注册");
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

}
